﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _05_Time_TrackBar
{
    public partial class Form1 : Form
    {
        //定义数组用来存储指定路径下的所有图片
        string[] imageURL = null;
        //定义一个变量用来存储当前图片的索引
        int index = 0;

        public Form1()
        {
            InitializeComponent();

            //设置当前滑块的值
            this.trackBar1.Value = 100;
            this.label2.Text = this.trackBar1.Value.ToString() + "MS";
            //读取指定路径下的所有图片
            imageURL = Directory.GetFiles(@"./xing");
            //指定第一张图片显示
            this.pictureBox1.ImageLocation = imageURL[0];



        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            this.label2.Text = this.trackBar1.Value.ToString()+"MS";
            //设置定时器的时间间隔
            this.timer1.Interval = this.trackBar1.Value;

        }
        /// <summary>
        /// 启动定时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //启动定时器
            this.timer1.Start();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //停止定时器
            this.timer1.Stop();
        }
        /// <summary>
        /// 定时器执行的方法(每隔一段时间固定执行的方法)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            //判断是否达到了最大值，如果没有达到最大值，就让索引+1
            if (index < imageURL.Length - 1)
            {
                this.pictureBox1.ImageLocation = imageURL[++index];
            }
            else
            {
                index = 0;
                this.pictureBox1.ImageLocation = imageURL[index];
            }

        }
    }
}
